package com.gmzg.gmzgoffice.works;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.gmzg.gmzgoffice.R;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SimpleCalendarDialogFragment extends AppCompatDialogFragment implements OnDateSelectedListener {


    private TextView textView;

    private String title;

    private setOnTimeListener mListener;
    private Date selectedDate;
    private SimpleDateFormat mDf;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setListener(setOnTimeListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();

        //inflate custom layout and get views
        //pass null as parent view because will be in dialog layout
        View view = inflater.inflate(R.layout.dialog_basic, null);
        mDf = new SimpleDateFormat("yyyy年MM月dd日");
        textView = (TextView) view.findViewById(R.id.textView);

        MaterialCalendarView widget = (MaterialCalendarView) view.findViewById(R.id.calendarView);
        selectedDate = Calendar.getInstance().getTime();
        widget.setSelectedDate(selectedDate);
        textView.setText(mDf.format(selectedDate));
        widget.setSelectionColor(Color.argb(255, 55, 157, 253));
        widget.setOnDateChangedListener(this);
        return new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setView(view)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mListener != null) {
                            mListener.setTime(selectedDate);
                        }
                    }
                })
                .create();
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        selectedDate  = date.getDate();
        textView.setText(mDf.format(date.getDate()));
    }

    public interface  setOnTimeListener{
        void setTime(Date date);
    }
}